package com.zm.openapi.utils;

import com.zm.openapi.exception.BusinessException;
import com.zm.openapi.enums.CommonEnum;
import com.zm.openapi.bo.AppSecretBo;
import com.zm.openapi.constants.OpenApiConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

@Slf4j
public class SignUtil {

    public static void checkSign(AppSecretBo secret, TreeMap<String, String> map, String sign) {
        if(StringUtils.isEmpty(sign)) {
            throw new BusinessException("sign 字段不能为空");
        }

        boolean res = Boolean.FALSE;
        String appid = secret.getAppid();
        String appSecret = secret.getAppsecret();

        if(MapUtils.isEmpty(map)) {
            log.error(CommonEnum.ERROR_REQUEST_PARAMETER_NULL.getMsg());
            throw new BusinessException(CommonEnum.ERROR_REQUEST_PARAMETER_NULL.getMsg());
        }

        String plainText = StringUtils.EMPTY;
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Iterator<Map.Entry<String, String>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Map.Entry<String, String> kv =  iterator.next();
            plainText += kv.getKey() + kv.getValue();
        }

        plainText += OpenApiConstants.APP_ID + appid + OpenApiConstants.APP_SECRET + appSecret;
        log.debug("toSigned plainText: " + plainText);

        // 我方签名结果
        String localSign = MD5Util.getMD5Code(plainText);
        //
        log.debug("localSign from plainText: " + localSign);
        if(!StringUtils.equals(sign, localSign)) {
            log.error(CommonEnum.ERROR_REQUEST_VALIDATE.getMsg() + ", plainText: " + plainText);
            throw new BusinessException(CommonEnum.ERROR_REQUEST_VALIDATE.getMsg());
        }
        log.debug("验签通过...");
    }

    public static void checkSign(AppSecretBo secret, String plainBody, String sign) {
        if(StringUtils.isEmpty(sign)) {
            throw new BusinessException("sign 字段不能为空");
        }

        String appid = secret.getAppid();
        String appSecret = secret.getAppsecret();

        if(StringUtils.isEmpty(plainBody)) {
            log.error(CommonEnum.ERROR_REQUEST_PARAMETER_NULL.getMsg());
            throw new BusinessException(CommonEnum.ERROR_REQUEST_PARAMETER_NULL.getMsg());
        }

        String plainText = plainBody + OpenApiConstants.APP_ID + appid + OpenApiConstants.APP_SECRET + appSecret;
        log.debug("toSigned plainText: " + plainText);

        // 我方签名结果
        String localSign = MD5Util.getMD5Code(plainText);
        //
        log.debug("localSign from plainText: " + localSign);
        if(!StringUtils.equals(sign, localSign)) {
            log.error(CommonEnum.ERROR_REQUEST_VALIDATE.getMsg() + ", plainText: " + plainText);
            throw new BusinessException(CommonEnum.ERROR_REQUEST_VALIDATE.getMsg());
        }
        log.debug("验签通过...");
    }
}
